package com.glsc.ngateway.opmanage.controller.report;

import com.glsc.ngateway.common.base.dto.opmanage.report.DerivativeResponse;
import com.glsc.ngateway.opmanage.aspect.UserCache;
import com.glsc.ngateway.opmanage.service.smartbi.BiUserManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/bi")
public class TokenController {

    Logger logger = LoggerFactory.getLogger(TokenController.class);

    @Autowired
    UserCache userCache;

    @Autowired
    BiUserManager biUserManager;

    @GetMapping("/gettoken")
    public DerivativeResponse getToken(@RequestParam(value = "biVersion", required = false) String biVersion, HttpServletRequest request) {
        DerivativeResponse response = null;
        try {
            String user = userCache.getOpNo(request);
            String token = biUserManager.getToken(user,biVersion);
            response = DerivativeResponse.success("ok");
            response.setData(token);
        } catch (Exception e) {
            logger.error(e.getMessage());
            response = DerivativeResponse.error(e.getMessage());
        }
        return response;

    }

}
